import Layout from "@/Layout";
import tableBasic from "@/Layout/tableBasic";
import Normal from "@/Layout/Normal";
import router from "@/router";
/**
 *  TODO 处理路由数据
 * @param asyncRouterMap 后台传过来的路由表
 * @returns {*}
 */
export function filterAsyncRouter(asyncRouterMap, father = undefined) {
  const accessedRouters = asyncRouterMap.filter((route, index) => {
    let componentName = route.component;
    if (componentName === "Layout") {
      route.component = Layout;
    } else if (componentName === "tableBasic") {
      route.component = tableBasic;
    } else if (componentName === "Normal") {
      route.component = Normal;
    } else {
      if (typeof componentName == "string" && componentName !== "") {
        route.component = Normal;
        route.component = () => import("@/views/" + componentName);
      } else {
        route.component = Normal;
      }
    }

    if (route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children, route);
    }
    return true;
  });

  return accessedRouters;
}

export function filter(asyncRouterMap, father) {
  const accessedRouters = asyncRouterMap.filter(route => {
    let componentName = route.component;
    if (route.meta.auth_type === 1 && route.component) {
      if (componentName === "Layout") {
        route.component = Layout;
      } else if (componentName === "tableBasic") {
        route.component = tableBasic;
      } else if (componentName === "Normal") {
        route.component = Normal;
      } else {
        if (componentName) {
          route.component = () => import("@/views/" + componentName);
        } else {
          route.component = Normal;
        }
      }
      if (route.meta.is_show === 0 && father) {
        father.children.push(route);
        router.addRoute(father.name, route);
        return false;
      }
    } else {
      return false;
    }
    if (route.children && route.children.length) {
      route.children = filter(route.children, route);
    }
    return true;
  });

  return accessedRouters;
}
